#include <BinaryTree.hpp>
#include <iostream>

using namespace std;

class Solution
{
  public:
    int sumNumbers(TreeNode *root)
    {
        if (root == nullptr)
        {
            return 0;
        }
        int number = 0;
        return dfs(root, number);
    }
    int dfs(TreeNode *root, int &number)
    {
        int count = 0;
        number *= 10;
        number += root->val;
        if (root->left == nullptr && root->right == nullptr)
        {
            count += number;
        }
        if (root->left != nullptr)
        {
            count += dfs(root->left, number);
        }
        if (root->right != nullptr)
        {
            count += dfs(root->right, number);
        }
        number /= 10;
        return count;
    }
};
int main()
{
    cout << "Code129" << endl;
    Solution s;
    TreeNode *root = CreatTree("1,2,3,4", "n");
    cout << s.sumNumbers(root) << endl;
    return 0;
}